{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "序列中的值:\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "序列中的索引:\n",
      "Index(['ID1', 'ID2', 'ID3', 'ID4', 'ID5', 'ID6', 'ID7', 'ID8', 'ID9'], dtype='object')\n",
      "访问序列的第1和第3上的值:\n",
      "ID1    1\n",
      "ID3    3\n",
      "dtype: int64\n",
      "访问序列索引为ID1和ID3上的值:\n",
      "ID1    1\n",
      "ID3    3\n",
      "dtype: int64\n",
      "判断ID1索引是否存在:True；判断ID10索引是否存在:False\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "data=Series([1,2,3,4,5,6,7,8,9],index=['ID1','ID2','ID3','ID4','ID5','ID6','ID7','ID8','ID9'])\n",
    "print('序列中的值:\\n{0}'.format(data.values))\n",
    "print('序列中的索引:\\n{0}'.format(data.index))\n",
    "print('访问序列的第1和第3上的值:\\n{0}'.format(data[[0,2]]))\n",
    "print('访问序列索引为ID1和ID3上的值:\\n{0}'.format(data[['ID1','ID3']]))\n",
    "print('判断ID1索引是否存在:%s；判断ID10索引是否存在:%s'%('ID1' in data,'ID10' in data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第2，3行：导入Pandas且指定别名为pd。导入pandas模块下的序列和数据框。\n",
    "（2）第4行：利用Pandas的函数Series()生成一个包含9个元素（取值为1至9）的序列，且指定各元素的索引名依次为ID1,ID2等。后续可通过索引访问相应元素。\n",
    "（3）第5行：序列的.values属性中存储着各元素的元素值。\n",
    "（4）第6行：序列的.index属性中存储着各元素的索引。\n",
    "（5）第7行：利用索引号（从0开始）访问指定元素。应以列表形式（如[0,2]）指定多个索引号。\n",
    "（6）第8行：利用索引名访问指定元素。索引名应用单引号括起来。应以列表形式（如[‘ID1’,’ID3’]）指定多个索引名。\n",
    "（7）第9行：利用Python运算符in，判断是否存在某个索引名。若存在判断结果为True（真），否则为False（假）。True和False是Python的布尔型变量的仅有的取值。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "date的类型：<class 'pandas.core.frame.DataFrame'>\n",
      "数据框的行索引：RangeIndex(start=0, stop=2155, step=1)\n",
      "数据框的列名：Index(['日期', 'AQI', '质量等级', 'PM2.5', 'PM10', 'SO2', 'CO', 'NO2', 'O3'], dtype='object')\n",
      "访问AQI和PM2.5所有值：\n",
      "      AQI  PM2.5\n",
      "0      81     45\n",
      "1     145    111\n",
      "2      74     47\n",
      "3     149    114\n",
      "4     119     91\n",
      "...   ...    ...\n",
      "2150  183    138\n",
      "2151  175    132\n",
      "2152   30      7\n",
      "2153   40     13\n",
      "2154   73     38\n",
      "\n",
      "[2155 rows x 2 columns]\n",
      "访问第2至3行的AQI和PM2.5：\n",
      "   AQI  PM2.5\n",
      "1  145    111\n",
      "2   74     47\n",
      "访问索引1至索引2的第2和4列：\n",
      "   AQI  PM2.5\n",
      "1  145    111\n",
      "2   74     47\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 2155 entries, 0 to 2154\n",
      "Data columns (total 9 columns):\n",
      "日期       2155 non-null datetime64[ns]\n",
      "AQI      2155 non-null int64\n",
      "质量等级     2155 non-null object\n",
      "PM2.5    2155 non-null int64\n",
      "PM10     2155 non-null int64\n",
      "SO2      2155 non-null int64\n",
      "CO       2155 non-null float64\n",
      "NO2      2155 non-null int64\n",
      "O3       2155 non-null int64\n",
      "dtypes: datetime64[ns](1), float64(1), int64(6), object(1)\n",
      "memory usage: 151.6+ KB\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "data=pd.read_excel('北京市空气质量数据.xlsx')\n",
    "print('date的类型：{0}'.format(type(data)))\n",
    "print('数据框的行索引：{0}'.format(data.index))\n",
    "print('数据框的列名：{0}'.format(data.columns))\n",
    "print('访问AQI和PM2.5所有值：\\n{0}'.format(data[['AQI','PM2.5']]))\n",
    "print('访问第2至3行的AQI和PM2.5：\\n{0}'.format(data.loc[1:2,['AQI','PM2.5']]))\n",
    "print('访问索引1至索引2的第2和4列：\\n{0}'.format(data.iloc[1:3,[1,3]]))\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第3行：利用Pandas函数read_excel()将一个Excel文件（北京市空气质量数据.xlsx）读入到数据框中。\n",
    "（2）第4行：利用Python函数type()浏览对象data的类型，结果显示为数据框。\n",
    "（3）第5，6行：数据框的.index和.columns属性中存储着数据框的行索引和列索引名。这里，行索引默认取值：0至样本量N-1。列索引名默认为数据文件中第一行的变量名。\n",
    "（4）第7行：利用列索引名访问指定变量。多个列索引名应以列表形式放在方括号中（[‘AQI’,’PM2.5’]）。\n",
    "（5）第8行：利用数据框的.loc属性访问指定行索引和变量名上的元素。\n",
    "注意：数据框对应二维表格，应给两个索引。\n",
    "（6）第9行：利用数据框的.iloc属性访问指定行索引和列索引号上的元素。\n",
    "注意：使用行索引时冒号:后的行不包括在内\n",
    "（7）第10行：利用数据框的info()方法显示数据框的行索引、列索引以及数据类型等信息。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "合并后的数据:\n",
      "  key  var1  var2\n",
      "0   a   0.0   NaN\n",
      "1   a   3.0   0.0\n",
      "2   d   1.0   NaN\n",
      "3   d   5.0   NaN\n",
      "4   c   2.0   2.0\n",
      "5   c   NaN   2.0\n",
      "6   c   6.0   2.0\n",
      "7   c   6.0   2.0\n",
      "8   b   4.0   1.0\n",
      "删除重复数据行后的数据:\n",
      "  key  var1  var2\n",
      "0   a   0.0   NaN\n",
      "1   a   3.0   0.0\n",
      "2   d   1.0   NaN\n",
      "3   d   5.0   NaN\n",
      "4   c   2.0   2.0\n",
      "5   c   NaN   2.0\n",
      "6   c   6.0   2.0\n",
      "8   b   4.0   1.0\n",
      "判断是否为缺失值:\n",
      "     key   var1   var2\n",
      "0  False  False   True\n",
      "1  False  False  False\n",
      "2  False  False   True\n",
      "3  False  False   True\n",
      "4  False  False  False\n",
      "5  False   True  False\n",
      "6  False  False  False\n",
      "8  False  False  False\n",
      "判断是否不为缺失值:\n",
      "    key   var1   var2\n",
      "0  True   True  False\n",
      "1  True   True   True\n",
      "2  True   True  False\n",
      "3  True   True  False\n",
      "4  True   True   True\n",
      "5  True  False   True\n",
      "6  True   True   True\n",
      "8  True   True   True\n",
      "删除缺失值后的数据:\n",
      "  key  var1  var2\n",
      "1   a   3.0   0.0\n",
      "4   c   2.0   2.0\n",
      "6   c   6.0   2.0\n",
      "8   b   4.0   1.0\n",
      "以均值替换缺失值:\n",
      "  key  var1  var2\n",
      "0   a   0.0   1.4\n",
      "1   a   3.0   0.0\n",
      "2   d   1.0   1.4\n",
      "3   d   5.0   1.4\n",
      "4   c   2.0   2.0\n",
      "5   c   3.0   2.0\n",
      "6   c   6.0   2.0\n",
      "8   b   4.0   1.0\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series,DataFrame\n",
    "df1=DataFrame({'key':['a','d','c','a','b','d','c'],'var1':range(7)})\n",
    "df2=DataFrame({'key':['a','b','c','c'],'var2':[0,1,2,2]})\n",
    "df=pd.merge(df1,df2,on='key',how='outer')\n",
    "df.iloc[0,2]=np.NaN\n",
    "df.iloc[5,1]=np.NaN\n",
    "print('合并后的数据:\\n{0}'.format(df))\n",
    "df=df.drop_duplicates()\n",
    "print('删除重复数据行后的数据:\\n{0}'.format(df))\n",
    "print('判断是否为缺失值:\\n{0}'.format(df.isnull()))\n",
    "print('判断是否不为缺失值:\\n{0}'.format(df.notnull()))\n",
    "print('删除缺失值后的数据:\\n{0}'.format(df.dropna()))\n",
    "fill_value=df[['var1','var2']].apply(lambda x:x.mean())\n",
    "print('以均值替换缺失值:\\n{0}'.format(df.fillna(fill_value)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "代码说明：\n",
    "（1）第4，5行：基于Python字典建立数据框。\n",
    "（2）第6行：利用Pandas函数merge()将两个数据框依指定关键字做横向合并，生成一个新数据框。\n",
    "（3）第7，8行：人为指定某样本观测的某变量值为NaN。\n",
    "（4）第10行：利用Pandas函数drop_duplicates()剔除数据框中在全部变量上均重复取值的样本观测。\n",
    "（5）第12，13行：利用数据框的.isnull()和.notnull()方法，对数据框中的每个元素判断其是否为NaN或不是NaN，结果为True或False。\n",
    "（6）第14行：利用数据框.dropna()方法剔除取NaN的样本观测。\n",
    "（7）第15行：利用数据框.apply()方法以及匿名函数计算各个变量的均值，并存储在名为fill_value的序列中。\n",
    "（8）第16行：利用数据框的.fillna()方法，将所有NaN替换为指定值（这里为fill_value）。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
